﻿Imports EShop.VBClass

Partial Class webprogram_adminadd
    Inherits System.Web.UI.Page


    Sub loadAdmin(aid As String)
        Dim sParameter() As Data.SqlClient.SqlParameter = { _
                       New Data.SqlClient.SqlParameter("@aid", aid) _
                   }
        Dim dbRead As DBReaders = GetDataReader("admins", "*", "id=@aid", "", sParameter, "adm_")
        If dbRead IsNot Nothing Then
            Me.TextBox_UID.Text = dbRead.dbItems(0, "Uid", "")
            Me.TextBox_Name.Text = dbRead.dbItems(0, "Name", "")
            Me.DropDownList_role.SelectedValue = dbRead.dbItems(0, "role", 0)
            Me.DropDownList_Lock.SelectedValue = getYesNoN(dbRead.dbItems(0, "lock", 0))
            Me.Label_Logintimes.Text = dbRead.dbItems(0, "Logintimes", "")
            Me.Label_lsLogintime.Text = dbRead.dbItems(0, "lsLogintime", "")
            Me.Label_lsLoginIP.Text = dbRead.dbItems(0, "lsLoginIP", "")
            If Session("admin_id") = aid Then
                Me.DropDownList_Lock.Enabled = False
            End If
        End If
    End Sub

    Sub saveAdmin()
        Dim sqlG As String = "", strTit As String = ""
        Dim cId, adm_Uid, adm_Name, adm_PWD As String
        Dim adm_lock As Integer = Me.DropDownList_Lock.SelectedValue
        Dim adm_role As Integer = Me.DropDownList_role.SelectedValue
        adm_PWD = Me.TextBox_PWD.Text
        adm_Uid = Me.TextBox_UID.Text
        adm_Name = Me.TextBox_Name.Text
        cId = Me.TextBox_Cid.Text
        If Me.Button_save.AccessKey = "A" Then
            If checkExist("admins", "adm_uid", "'" & adm_Uid & "'") Then
                ExecuteJSalert(Me, "用户名[" & adm_Uid & "]已经存在,请修改成其他名称！")
                Exit Sub
            End If
            strTit = "添加"
            If adm_PWD.Trim = "" Then
                adm_PWD = GetCodeString("123456", "zyusoft")
            Else
                adm_PWD = GetCodeString(adm_PWD, "zyusoft")
            End If
            sqlG = "insert into admins (adm_Name, adm_Uid, adm_lock, adm_role,adm_PWD)" & _
                               " values(@adm_Name,@adm_Uid,@adm_lock,@adm_role,@adm_PWD)"

        ElseIf Me.Button_save.AccessKey = "M" Then
            If checkExist("admins", "adm_uid", "'" & adm_Uid & "'", "id<>" & cId) Then
                ExecuteJSalert(Me, "用户名[" & adm_Uid & "]已经存在,请修改成其他名称！")
                Exit Sub
            End If
            strTit = "修改"

            sqlG = "update admins set adm_Name=@adm_Name,adm_Uid=@adm_Uid, adm_lock=@adm_lock,adm_role=@adm_role "
            If adm_PWD.Trim <> "" Then
                adm_PWD = GetCodeString(adm_PWD, "zyusoft")
                sqlG &= ",adm_PWD=@adm_PWD"
            End If
            sqlG &= " where id=" & cId
        End If

        Dim sParameter() As Data.SqlClient.SqlParameter = { _
                        New Data.SqlClient.SqlParameter("@adm_Name", adm_Name) _
                      , New Data.SqlClient.SqlParameter("@adm_Uid", adm_Uid) _
                      , New Data.SqlClient.SqlParameter("@adm_lock", adm_lock) _
                      , New Data.SqlClient.SqlParameter("@adm_role", adm_role) _
                     }
        If adm_PWD.Trim <> "" Then
            Dim spLen As Integer = sParameter.Length
            ReDim Preserve sParameter(spLen)
            sParameter(spLen) = New Data.SqlClient.SqlParameter("@adm_PWD", adm_PWD)
        End If
        If ExecuteSQL(sqlG, sParameter) Then
            ExecuteJSalert(Me, strTit & "管理员成功！")
            ExecuteJS(Me, "goUrl('admins.aspx')")
        Else
            ExecuteJSalert(Me, strTit & "管理员出错！")
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        checkSessionTimeout(Me)
        checkData(Me)

        If Not Me.IsPostBack Then
            Dim sysRole As Integer = Session("admin_Role")
            If Session("admin_UID") <> "wen" Then
                For i As Integer = 0 To sysRole - 2
                    Me.DropDownList_role.Items(i).Attributes.Add("disabled", True)
                Next
            End If

            Dim aid As String = dValue(Request("aid"), "")
            Me.TextBox_Cid.Text = aid
            If aid.Trim <> "" Then
                Me.Button_save.AccessKey = "M"
                Me.Button_save.Text = "修改"
                loadAdmin(aid)
            Else
                If sysRole > 2 Then
                    ExecuteJSalert(Me, "对不起！您没有管理用户的权限！")
                    Me.Button_save.Enabled = False
                    Exit Sub
                End If
                Me.Button_save.AccessKey = "A"
                Me.Button_save.Text = "添加"
            End If
        End If
    End Sub

    Protected Sub Button_save_Click(sender As Object, e As EventArgs) Handles Button_save.Click
        saveAdmin()
    End Sub
End Class
